//
//  WBNetWorkManager+Extension.swift
//  个人微博
//
//  Created by 任聪聪 on 2017/10/9.
//  Copyright © 2017年 任孜聪. All rights reserved.
//

import Foundation

extension WBNetWorkManager{
    //定义用户新浪微博代码
    /// //封装新浪微博网络请求
    ///   - since_id: 返回ID比since_id大的微博（即比since_id时间晚的微博），默认为0/实现上拉刷新
    ///   - max_id: 返回ID小于或等于max_id的微博，默认为0／实现下拉刷新
    /// - Parameter completion: 完成回调的数据字典／是否成功获取到数据
    func statusList(since_id:Int64 = 0,max_id:Int64 = 0, completion:@escaping ([[String:AnyObject]]?,_ isSucess:Bool)->()) {
        let params = ["since_id":"\(since_id)","max_id":"\(max_id)"]
        //利用网络工具发起GET请求加载数据
        let URLString = "https://api.weibo.com/2/statuses/home_timeline.json"
        AcessTokenRequest(URLString: URLString, parameters : params) { (json,isSucess ) in
            let result = json?["statuses"] as? [[String:AnyObject]]
            completion(result,isSucess)
        }
    }
    //定义未读取的新浪微博数量方法
    func unReadCount(compeletion:@escaping (_ count:Int)->()) {
        guard let uid = userAccount.uid else {
            return
        }
        let url = "https://rm.api.weibo.com/2/remind/unread_count.json";
        let param = ["uid":uid]
        AcessTokenRequest(URLString: url, parameters: param) { (json, isSucess) in
            guard let data = json as? [String:AnyObject] else{
                return
            }
            let count = data["status"] as! Int
           compeletion(count)
        }
    }
}

// MARK: - 用户微博信息
extension WBNetWorkManager{
    
    /// 加载用户微博信息方法
    func loadUserInfo(compeletion:@escaping ([String:AnyObject])->()) {
        let urlString = "https://api.weibo.com/2/users/show.json"
        let param = ["uid":userAccount.uid]
        AcessTokenRequest(URLString: urlString, parameters: param) { (json, isSucess) in
            //完成回调
            compeletion(json as? [String:AnyObject] ?? [:] )
        }
    }
}
extension WBNetWorkManager{
    //通过授权码获取acessToken
    //发起网络请求
    //异步调用，需要什么返回什么
    /// <#Description#>
    ///
    /// - Parameters:
    ///   - code: 授权码
    ///   - completion: 通过闭包返回是否登录成功
    func getacessToken(code:String,completion:@escaping (_ isSucess:Bool)->()) {
        let urlString = "https://api.weibo.com/oauth2/access_token"
        let param = ["client_id":WebAppKey,
                     "client_secret":WebAppSecret,
                     "grant_type":"authorization_code",
                     "code":code,
                     "redirect_uri":WebRedirctUri]
        requestData(type: .POST, URLString: urlString, parameters: param as [String : AnyObject]) { (json, isSucess) in
            //通过将字典的值设置到模型中去
            self.userAccount.yy_modelSet(with: json as? [String:AnyObject] ?? [:])
            //加载微博用户账户信息
            self.loadUserInfo(compeletion: { (dict) in
                //用通过闭包调用的用户字典信息来设置用户账户(用户头像和用户昵称)
                self.userAccount.yy_modelSet(withJSON: dict)
                //设置完用户账户信息之后在保存
                self.userAccount.saveUserAccount()
                print("信息：\(self.userAccount)")
                //调用闭包,闭包调用失败对用户信息没有影响
                completion(isSucess)
            })
        }
    }
}
